#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
    system("cd ../build && python3 gen.py 192.168.2.112 8080");
    system("objcopy -O binary ../build/main bin");
    int bfd = open("./bin", O_RDONLY);
    if (bfd == -1)
    {
        perror("open");
        return errno;
    }

    struct stat stat;
    if (fstat(bfd, &stat) == -1)
    {
        perror("fstat");
        return errno;
    };

    void *addr = mmap(0, stat.st_size, PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
    if ((int64_t)addr == -1)
    {
        perror("mmap");
        return errno;
    }

    if (read(bfd, addr, stat.st_size) == -1)
    {
        perror("read");
        return errno;
    };
    asm volatile(
        "jmp *%0\n"
        :
        : "a"(addr)
        :);

    // never reached
    return 0;
}